package org.lyq.erp.process.freighttemp.controller;

import java.math.BigDecimal;
import java.util.List;
import javax.annotation.Resource;
import org.lyq.erp.process.freighttemp.pojo.TblWxFreightTemplate;
import org.lyq.erp.process.freighttemp.service.TblWxFreightTemplateService;
import org.lyq.erp.utils.string.ObjectEmptyUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;


@Controller
@RequestMapping({"/TblWxFreightTemplate"})
public class TblWxFreightTemplateController
{
  private static Logger logger = LoggerFactory.getLogger(TblWxFreightTemplateController.class);
  
  @Resource
  private TblWxFreightTemplateService tblWxFreightTemplateService;

  
  @RequestMapping({"/list"})
  public String list(ModelMap map) {
    List<TblWxFreightTemplate> freightTempList = this.tblWxFreightTemplateService.selectAllFreightTemplate();
    map.put("freightTempList", freightTempList);
    return "process/freightTemp/freightTemp_list";
  }


  
  @RequestMapping({"/addFritTemp"})
  public String addFritTemp() { return "process/freightTemp/freightTemp_add"; }


  
  @RequestMapping({"/addFritTempSubmit"})
  public String addFritTempSubmit(String menuUuid, TblWxFreightTemplate freightTemp, ModelMap map) {
    if (!addAndUpdateValidation(freightTemp)) {
      map.put("dataBean", freightTemp);
      return "process/freightTemp/freightTemp_add";
    } 
    try {
      if (freightTemp.getFreightTempId() == null) {
        this.tblWxFreightTemplateService.insert(freightTemp);
      } else {
        this.tblWxFreightTemplateService.update(freightTemp);
      } 
    } catch (Exception e) {
      logger.info(e.getMessage());
      freightTemp.addErrorMessage("freightTemp006");
      freightTemp.popuperrorMessage = "true";
      map.put("dataBean", freightTemp);
      List<TblWxFreightTemplate> freightTempList = this.tblWxFreightTemplateService.selectAllFreightTemplate();
      map.put("freightTempList", freightTempList);
      return "process/freightTemp/freightTemp_list";
    } 
    return "redirect:/TblWxFreightTemplate/list?menuUuid=" + menuUuid;
  }
  
  @RequestMapping({"/updateFritTemp"})
  public String updateFritTemp(ModelMap map, TblWxFreightTemplate freightTemp) {
    if (freightTemp.getFreightTempId() != null) {
      freightTemp = this.tblWxFreightTemplateService.selectByFreightTempId(freightTemp.getFreightTempId());
      map.put("dataBean", freightTemp);
      return "process/freightTemp/freightTemp_update";
    } 
    freightTemp.addErrorMessage("freightTemp007");
    map.put("dataBean", freightTemp);
    return "process/freightTemp/freightTemp_add";
  }
  
  @RequestMapping({"/delete"})
  public String delete(TblWxFreightTemplate freightTemp, Integer menuUuid, ModelMap map) {
    if (freightTemp.getFreightTempId() != null) {
      try {
        this.tblWxFreightTemplateService.delete(freightTemp.getFreightTempId());
      }
      catch (Exception e) {
        logger.info(e.getMessage());
      } 
    }
    return "redirect:/TblWxFreightTemplate/list?menuUuid=" + menuUuid;
  }

  
  private boolean addAndUpdateValidation(TblWxFreightTemplate freightTemp) {
    if (ObjectEmptyUtils.isEmptyString(freightTemp.getFreightTempName())) {
      freightTemp.addErrorMessage("freightTempName", "freightTemp001");
    }
    if (ObjectEmptyUtils.isEmptyString(freightTemp.getFreightTempDesc())) {
      freightTemp.addErrorMessage("freightTempDesc", "freightTemp002");
    }
    if (ObjectEmptyUtils.isEmptyArray(freightTemp.getAddCodeNames()) || 
      ObjectEmptyUtils.isEmptyArray(freightTemp.getAddCodes())) {
      freightTemp.addErrorMessage("freightTemp005");
    }
    if (ObjectEmptyUtils.isEmptyArray(freightTemp.getFreightFirstPriorityAmts())) {
      freightTemp.addErrorMessage("freightTemp003");
    }
    if (ObjectEmptyUtils.isEmptyArray(freightTemp.getFreightContinWeightAmts())) {
      freightTemp.addErrorMessage("freightTemp004");
    }

    
    int alllength = freightTemp.getAddCodeNames().length + freightTemp.getAddCodes().length + freightTemp.getFreightFirstPriorityAmts().length + freightTemp.getFreightContinWeightAmts().length;
    int length = alllength / 4;
    if (alllength % 4 != 0 || length != freightTemp.getAddCodeNames().length || length != freightTemp
      .getAddCodes().length || length != freightTemp
      .getFreightFirstPriorityAmts().length || length != freightTemp
      .getFreightContinWeightAmts().length) {
      freightTemp.addErrorMessage("freightTemp006");
    }
    
    if (!ObjectEmptyUtils.isEmptyArray(freightTemp.getFreightFirstPriorityAmts()) && 
      !ObjectEmptyUtils.isEmptyArray(freightTemp.getFreightContinWeightAmts())) {
      if (freightTemp.getFreightFirstPriorityAmts().length != freightTemp.getFreightContinWeightAmts().length) {
        if (freightTemp.getFreightFirstPriorityAmts().length > freightTemp
          .getFreightContinWeightAmts().length) {
          freightTemp.addErrorMessage("freightTemp004");
        } else {
          freightTemp.addErrorMessage("freightTemp003");
        } 
      } else {
        BigDecimal[] freightFirstPriorityAmts = freightTemp.getFreightFirstPriorityAmts();
        BigDecimal[] freightContinWeightAmts = freightTemp.getFreightContinWeightAmts();
        for (int i = 0; i < freightContinWeightAmts.length; i++) {
          if (freightFirstPriorityAmts[i] == null) {
            freightTemp.addErrorMessage("freightTemp003");
          }
          if (freightContinWeightAmts[i] == null) {
            freightTemp.addErrorMessage("freightTemp004");
          }
        } 
      } 
    }
    return freightTemp.getMap().isEmpty();
  }
}
